<?php
class Consignment_takeout extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model("mmaster");
		$this->load->model("mconsignment");
		$this->load->model("msell");
		$this->load->library('enc');
		$this->load->library('fpdf');
	}

	public function index($consignment_id="") {
		$data['template']   = "shell/smooth";

		$consignment_id = $this->enc->defog($consignment_id);

		if (!$consignment_id) {
			$data['main_view']	= 'f-consignments'; // tampilkan daftar consignment yang belum ditakeout
		}
		else {
			$data['main_view'] 	= 'f-consign-detail';
			$data['detail']	= $this->mconsignment->detail($consignment_id);
			$data['items']	= $this->mconsignment->items($consignment_id);
		}

		$this->load->view($data['template'], $data);
	}


	public function takeout($consignment_id) {
		$itemStatus = $this->input->post("status");
		$items = $this->input->post("iccid");
		$this->db->trans_start();

			$saveTakeout = $this->mconsignment->takeout($consignment_id);
				$sold=0; $unsold=0;
				$stat=array(); $item=array();
				foreach ($itemStatus as $status) {
					$stat[] = $status;
					if($status == 0) {$sold++;}
					else {$unsold++;}
				}
				foreach ($items as $iccid) { $item[] = $iccid; }

			$newReceipt=""; $newSellIn="";
			echo "Laku = ".$unsold." ga Laku = ".$sold."<br>";
			if($sold != 0)	{$newSellIn = $this->mconsignment->createSellIn($consignment_id);}		// ada yang terjual
			// if($unsold != 0) {$newReceipt = $this->mconsignment->createReceipt($consignment_id);}	// jika tidak terjual
			

			$solditem = 0; $unsolditem = 0;
			for ($i=0; $i < count($items); $i++) { 
				if ($stat[$i] == 0) { // ini barang yang kejual
					$solditems = $this->mconsignment->sellInDetail($newSellIn, $item[$i], $consignment_id);
					$solditem++;
				}
				elseif ($stat[$i] == 1) { // ini barang yang tidak terjual
					// $receipts  = $this->mconsignment->receiptDetail($newReceipt, $item[$i], $consignment_id);
					$unsolditems = $this->mconsignment->normalize($item[$i]);
					$unsolditem++;
				}
			}
			$this->session->set_flashdata('civ_class', 'success');
			if ($solditem != 0 ) { $this->session->set_flashdata('message1', $solditem.'item terjual, telah tercatat sebagai Sell In');}
			if ($unsolditem != 0 ) { $this->session->set_flashdata('message2', $unsolditem.'item tidak terjual, telah dicatat untuk pengembalian');}
		
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
        } else {
            $this->db->trans_commit();
            redirect("index.php/consignment_list");
        }

	}

	function pdf($consignment_id){
        //decode $consignment_id
        $consignment_id = $this->enc->defog($consignment_id);
        
		$data['detail']	= $this->mconsignment->detail($consignment_id);
		$data['items']	= $this->mconsignment->items($consignment_id);

		$tgl = explode(" ", $data['detail']->consignment_date);
		$duedate = strtotime(date("Y-m-d", strtotime($tgl[0])) . " +".$data['detail']->term_day." day");
		if($data['detail']->istatus == 1){
			$status = 'Open';
		}
		else {$status = 'Close';}
		

		$this->fpdf->Open();
		$this->fpdf->AddPage();
		$this->fpdf->SetAutoPageBreak(true, 5);
		$this->fpdf->SetMargins(10, 10, 10, 10);
		$this->fpdf->AliasNbPages();

		$this->fpdf->Image(FCPATH.'file/images/logo_smartfren.png' );
		$this->fpdf->SetFont('Arial','B',12);

		$this->fpdf->Cell(0,-15, "Nota Penitipan Barang",0,0,'R' );
		$this->fpdf->SetFont('Arial','',8);$this->fpdf->ln(1);
		$this->fpdf->Cell(0,-5, "(Consignment Note)",0,0,'R' );
		$this->fpdf->Line(10, 30, 200, 30);

		$this->fpdf->ln(5);
		$this->fpdf->SetFont('Arial','',10);
		$this->fpdf->Cell(25,8,'Territory',1,0,'L');
		$this->fpdf->Cell(50,8,$data['detail']->territory,1,0,'L');
		$this->fpdf->Cell(0,8,'Tanggal: '.date("d F Y", strtotime($tgl[0])),0,1,'R');

		$this->fpdf->Cell(25,8,'Nama Sales',1,0,'L');
		$this->fpdf->Cell(50,8,$data['detail']->user_id.' | '.$data['detail']->user_group_caption.' | '.$data['detail']->user_name ,1,0,'L');
		$this->fpdf->Cell(0,8,'No: '.substr("0000000" . $consignment_id, -8),0,1,'R');

		$this->fpdf->Cell(25,8,'Partner',1,0,'L');
		$this->fpdf->Cell(50,8,$data['detail']->territory_id.' | '.$data['detail']->territory_name,1,1,'L');

		$this->fpdf->Cell(25,8,'Jatuh Tempo',1,0,'L');
			$this->fpdf->Cell(50,8,date("d F Y", $duedate),1,1,'L');

		$this->fpdf->Cell(25,8,'Status',1,0,'L');
		$this->fpdf->Cell(50,8,$status,1,1,'L');

		$this->fpdf->ln(5);
		$this->fpdf->Cell(8,8,'No',1,0,'C');
		$this->fpdf->Cell(35,8,'ICCID',1,0,'C');
		$this->fpdf->Cell(70,8,'Nama Item',1,0,'C');
		$this->fpdf->Cell(35,8,'Group',1,0,'C');
		$this->fpdf->Cell(25,8,'Harga',1,0,'C');
		$this->fpdf->Cell(15,8,'Status',1,1,'C');

		$x=1;$this->fpdf->SetFont('Arial','',8);
		foreach ($data['items'] as $item) {
			$this->fpdf->Cell(8,7,$x,1,0,'C');
			$this->fpdf->Cell(35,7,$item->iccid,1,0,'C');
			$this->fpdf->Cell(70,7,$item->item_name,1,0,'L');
			$this->fpdf->Cell(35,7,$item->item_group_name,1,0,'L');
			$this->fpdf->Cell(25,7,'Rp '.number_format($item->price, 2, ',', '.'),1,0,'R');
				if ($item->istatus == 3) { $sold = 'Terjual'; }
				else if ($item->istatus == 2) { $sold = 'Terjual'; }
				else { $sold = 'Konsinyasi'; }
			$this->fpdf->Cell(15,7,$sold,1,1,'C');
			$x++;
		}
		
		$this->fpdf->ln(5);
		$this->fpdf->SetFont('Arial','B',10);
		$this->fpdf->Write(12, 'Remark');$this->fpdf->ln(5);
		$this->fpdf->SetFont('Arial','',10);
		$this->fpdf->Write(12, $data['detail']->remark);

		$this->fpdf->ln(20);
		$this->fpdf->Cell(50,20,'Mengetahui CC/CA',1,0,'L');$this->fpdf->Cell(50,20,' ',1,1,'L');
		$this->fpdf->Cell(50,20,'SFA/DS',1,0,'L');$this->fpdf->Cell(50,20,' ',1,1,'L');

		$this->fpdf->Output(); 
	}

} //End of Class